﻿Public Class seg_fcontrol
    Public nuevo As Integer
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            modificar.Enabled = True
            eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True

        End If
    End Sub

    Private Sub PanelArriba_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PanelArriba.Paint

    End Sub

    Private Sub Seg_controlesBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles Seg_controlesBindingNavigatorSaveItem.Click

        Try
            Me.Validate()
            Me.Seg_controlesBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, My.Resources.StringGlobal.msjNotificacion, My.Resources.StringGlobal.ErrorValidarDatos)
            TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, My.Resources.StringGlobal.prgVinicio, "Error al validar los datos.", ex.Message, My.Resources.StringGlobal.mdlSeguridad, Date.Today, TimeOfDay.TimeOfDay.ToString, My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
        End Try
       

    End Sub

    Private Sub seg_fcontrol_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.seg_controles' table. You can move, or remove it, as needed.
        Try
            Me.Seg_controlesTableAdapter.Fill(Me.DbFireAndIceDataSet.seg_controles)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, My.Resources.StringGlobal.msjNotificacion, My.Resources.StringGlobal.ErrorCargarDatos)
            TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, My.Resources.StringGlobal.prgVinicio, "Error al cargar los datos en el LOAD del formulario.", ex.Message, My.Resources.StringGlobal.mdlSeguridad, Date.Today, TimeOfDay.TimeOfDay.ToString, My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
        End Try

    End Sub

    Private Sub agregar_Click(sender As System.Object, e As System.EventArgs) Handles agregar.Click
        nuevo = 1
        BindingNavigatorAddNewItem.PerformClick()
        Mostrar()
    End Sub

    Private Sub modificar_Click(sender As System.Object, e As System.EventArgs) Handles modificar.Click
        nuevo = 0
        Mostrar()

    End Sub

    Private Sub eliminar_Click(sender As System.Object, e As System.EventArgs) Handles eliminar.Click

        Dim opcion As Integer
        opcion = MsgBox(My.Resources.StringGlobal.msjConfirmarEliminarRegistro, MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try
                Seg_controlesTableAdapter.DeleteQueryControl(IdcontrolTextBox.Text)
                Me.Seg_controlesTableAdapter.Fill(Me.DbFireAndIceDataSet.seg_controles)
                AlertControl1.AppearanceCaption.ForeColor = Color.Green
                AlertControl1.Show(Me, My.Resources.StringGlobal.msjNotificacion, My.Resources.StringGlobal.msjEliminóRegistro)
            Catch ex As Exception
                AlertControl1.AppearanceCaption.ForeColor = Color.Red
                AlertControl1.Show(Me, My.Resources.StringGlobal.msjNotificacion, My.Resources.StringGlobal.msjNoEliminóRegistroPorRelacion)
                TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, My.Resources.StringGlobal.prgVinicio, "Error al eliminar un control", ex.Message, My.Resources.StringGlobal.mdlSeguridad, Date.Today, TimeOfDay.TimeOfDay.ToString, My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
            End Try
        End If

    End Sub

    Private Sub guardar_Click(sender As System.Object, e As System.EventArgs) Handles guardar.Click
        Dim opcion As Integer

        Dim existe As Integer 'Variable que identifica si el ID ya existe o no

        Try
            existe = Me.Seg_controlesTableAdapter.ScalarQueryDevuelveCeroSiElIdcontrolNoExiste(IdcontrolTextBox.Text.Trim)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, My.Resources.StringGlobal.msjNotificacion, "Error al consultar si el control ya existe en la base de datos.")
            TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, My.Resources.StringGlobal.prgVinicio, "Error al consultar si el control ya existe en la base de datos.", ex.Message, My.Resources.StringGlobal.mdlSeguridad, Date.Today, TimeOfDay.TimeOfDay.ToString, My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
        End Try

        If IdcontrolTextBox.Text.Length = 0 Or String.IsNullOrWhiteSpace(IdcontrolTextBox.Text) Then
            ErrorProvider1.SetError(IdcontrolTextBox, "Debe ingresar un ID para el control")
            Return
        End If
        ErrorProvider1.SetError(IdcontrolTextBox, Nothing)

        If nuevo = 1 Then
            If existe <> 0 Then
                ErrorProvider1.SetError(IdcontrolTextBox, "El control ya existe")
                Return
            End If
            ErrorProvider1.SetError(IdcontrolTextBox, Nothing)
        End If

        If NombrecontrolTextBox.Text.Length = 0 Then
            ErrorProvider1.SetError(NombrecontrolTextBox, "Debe ingresar un nombre para el control")
            Return
        End If
        ErrorProvider1.SetError(NombrecontrolTextBox, Nothing)

        IdcontrolTextBox.Text = IdcontrolTextBox.Text.Trim 'Validar que solo mande un ID sin espacios en blanco
        Seg_controlesBindingNavigatorSaveItem.PerformClick()
        AlertControl1.AppearanceCaption.ForeColor = Color.Green
        AlertControl1.Show(Me, My.Resources.StringGlobal.msjNotificacion, My.Resources.StringGlobal.msjGuardóRegistro)

        Mostrar()

    End Sub

    Private Sub cancelar_Click(sender As System.Object, e As System.EventArgs) Handles cancelar.Click
        ErrorProvider1.SetError(IdcontrolTextBox, Nothing)
        ErrorProvider1.SetError(NombrecontrolTextBox, Nothing)
        Mostrar()
        Try
            Me.Seg_controlesTableAdapter.Fill(Me.DbFireAndIceDataSet.seg_controles)
        Catch ex As Exception
            AlertControl1.AppearanceCaption.ForeColor = Color.Red
            AlertControl1.Show(Me, My.Resources.StringGlobal.msjNotificacion, My.Resources.StringGlobal.ErrorCargarDatos)
            TlogTableAdapter1.InsertQueryLogs(TlogTableAdapter1.ScalarQueryCountLogs + 1, My.Forms.MDI.Iduser.Caption, My.Resources.StringGlobal.prgVinicio, "Error al cargar los datos cuando pulsó cancelar.", ex.Message, My.Resources.StringGlobal.mdlSeguridad, Date.Today, TimeOfDay.TimeOfDay.ToString, My.Computer.Name, Name.ToString, sender.GetType.FullName.ToString)
        End Try

    End Sub

    Private Sub imprimir_Click(sender As System.Object, e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()
    End Sub
End Class